package Chapter15;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Comparator;

/**
 * @author 杨森
 * @date 2022年10月29日 14:27
 */
public class GenericExercise02 {
    public static void main(String[] args) {
        ArrayList<Employee> employees = new ArrayList<>();
        employees.add(new Employee("tom",20000,new MyDate(2000,11,11)));
        employees.add(new Employee("jack",120000,new MyDate(2001,11,11)));
        employees.add(new Employee("tom",50000,new MyDate(1990,11,11)));

        System.out.println(employees);
        
        employees.sort(new Comparator<Employee>() {
            @Override
            public int compare(Employee emp1, Employee emp2) {
                if (!(emp1 instanceof Employee&&emp2 instanceof Employee)){
                    System.out.println("类型不正确");
                    return 0;
                }
//                比较name
                int i=emp1.getName().compareTo(emp2.getName());
                if (i != 0) {
                    return i;
                }
//                如果name相同，就比较birthday 封装在MyDate中
                return emp1.getBirthday().compareTo(emp2.getBirthday());


            
            }
        });

        System.out.println("排序后");
        System.out.println(employees);


    }
}

class MyDate implements Comparable<MyDate>{
    private int year;
    private int month;
    private int day;

    public MyDate(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("MyDate{");
        sb.append("year=").append(year);
        sb.append(", month=").append(month);
        sb.append(", day=").append(day);
        sb.append('}');
        return sb.toString();
    }

    @Override
    public int compareTo(MyDate o) {
        int yearMinus = year-o.getYear();
        if (yearMinus!=0){
            return yearMinus;
        }
//                如果year相同，就比较month
        int monthMinus=month - o.getMonth();
        if (monthMinus!=0){
            return monthMinus;
        }
//                如果month相同，就比较day
        return day- o.getDay();
    }
}
class Employee{
    private String name;
    private double sal;
    private MyDate birthday;

    public Employee(String name, double sal, MyDate birthday) {
        this.name = name;
        this.sal = sal;
        this.birthday = birthday;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSal() {
        return sal;
    }

    public void setSal(double sal) {
        this.sal = sal;
    }

    public MyDate getBirthday() {
        return birthday;
    }

    public void setBirthday(MyDate birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("Employee{");
        sb.append("name='").append(name).append('\'');
        sb.append(", sal=").append(sal);
        sb.append(", birthday=").append(birthday);
        sb.append('}').append("\n");
        return sb.toString();
    }
}

